﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace Web
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {
            if (!string.IsNullOrWhiteSpace( Request.QueryString["logout"]))
            {
                Session.Abandon();
                Response.Redirect( "login.aspx", true );
            }

            var store = BasePage.GetDataStore();
            if (!store.AccountAny())
                btnLogin.Text = "Create";
        }

        protected void btnLogin_Click( object sender, EventArgs e )
        {
            if (string.IsNullOrWhiteSpace( txtPassword.Text ))
                return;

            if (string.IsNullOrWhiteSpace( txtEmail.Text ))
                return;
            
            var store = BasePage.GetDataStore();
            var account = store.AccountLogin( txtEmail.Text, txtPassword.Text );

            if (account != null)
                Visitor.Current.Login( account );

            if (!Visitor.Current.IsAnonymous)
                Response.Redirect( "sites.aspx" );

        }
    }
}